package broker_utils

import (
	"fmt"
	"hft/Database/admin"
	"regexp"
	"strings"
)

func OperaData(data *admin.Account, role *admin.Role, account *admin.Account) *admin.Account {
	data.RoleId = role.ID
	data.Link = fmt.Sprintf("%v%v,", role.Link, role.ID)
	data.AccountLink = fmt.Sprintf("%v%v,", account.AccountLink, account.ID)
	data.PID = account.ID
	data.RoleName = role.RoleName
	data.RegionLink = account.RegionLink
	account.AccountExpand.Sort = 1
	account.AccountExpand.CanBindLouPan = true
	account.AccountExpand.IsAudit = 1
	account.AccountExpand.Score = 0
	return data
}

// 判断是否需要添加音频以及音频
func IsNeedAddAudio(audit int, ID uint) (string, string) {
	if audit == 2 {
		return fmt.Sprintf("%v_ani.wav", ID), fmt.Sprintf("%v_dni.wav", ID)
	} else {
		return "", ""
	}
}

// 判断当前的经纪人手机号码的情况
func BrokerPhone(managerPhone string, oldBroker string, shouldReplacePhone string) string {
	var newBroker = strings.Replace(oldBroker, fmt.Sprintf(",%v,", oldBroker), ",", -1)
	//如果替换后没有了就要默认添加上经纪人的手机
	if newBroker == "," {
		return managerPhone
	} else {
		// 如果有的话就不用管直接返回
		var phoneLen = strings.Split(newBroker, ",")
		return strings.Join(phoneLen[1:len(phoneLen)-1], "|")
	}
}

// 添加新的经纪人手机
func AddNewBroker(managerPhone, oldBroker, shouldReplacePhone string) string {
	if oldBroker == fmt.Sprintf(",%v,", managerPhone) {
		return shouldReplacePhone
	} else {
		var newPhoneLen = strings.Split((oldBroker + fmt.Sprintf("%v,", shouldReplacePhone)), ",")
		return strings.Join(newPhoneLen[1:len(newPhoneLen)-1], "|")
	}
}

// 判断是否可以操作这个账号
func CanOperaThisAccount(managerLink string, accountLink string) bool {
	match, _ := regexp.MatchString(fmt.Sprintf("^%v", managerLink), accountLink)
	return match
}

// 转换状态
func ChangeStatus(status bool) int {
	fmt.Println(status)
	if status {
		return 1
	} else {
		return 0
	}
}

// 去掉图片开头
func RemovePrefix(str, prefix string) string {
	if strings.HasPrefix(str, prefix) {
		return str[len(prefix):] // 去掉前缀
	}
	return str // 原字符串没有指定前缀，返回原字符串
}
